package server;

import java.io.File;
import java.util.ArrayList;

import util.ChecksumHelper;
import util.SHAHelper;

import client.ClientFile;
import dataContracts.SharedDirectory;

public class ServerDirectory extends SharedDirectory {

	public ServerDirectory(String directoryPath) {
		super(directoryPath);
		
		loadFiles();
	}
	
	public void loadFiles() {
		files.clear();
		
		if (directory.isDirectory()) {
	    	for (File f : directory.listFiles()) {
	    		if (!f.isHidden())
	    			this.files.add(new ClientFile(f));
			}
    	}
		
		setChecksum(ChecksumHelper.GetChecksum(directory));
	}

	public boolean fileExists(String fileName) {
		for (ClientFile file : files) {
			if (file.getName().equalsIgnoreCase(fileName))
				return true;
		}
		
		return false;
	}
	
	public boolean fileChanged(String fileName, String checksum) {
		for (ClientFile file : files) {
			if (file.getName().equalsIgnoreCase(fileName) && file.getChecksum().equalsIgnoreCase(checksum)) 
				return false;
		}
		
		// return true if nothing was found so file is downloaded anyway (hack?)
		return true;
	}
}
